package com.atguigu.szdz.dcsyl.controller;


import com.atguigu.szdz.dcsyl.common.utils.LasFile;
import com.atguigu.szdz.dcsyl.common.utils.LasParser;
import com.atguigu.szdz.dcsyl.common.utils.R;
import com.atguigu.szdz.dcsyl.service.CurveDataService;
import com.atguigu.szdz.dcsyl.utils.MapperUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/importFile")
public class CurveDataController {

    @Autowired
    private CurveDataService curveDataService;
    @Autowired
    private MapperUtils mapperUtils;


    @Operation(summary = "上传文件并解析")
    @PostMapping("/uploadFile")
    public R uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("well_id") Integer wellId) {
        //传进来的wellId可能是异常值,先判断是否为空
        if (wellId == null) {
            return R.error("井ID不能为空");
        }
        //先去数据库查询是否有该井
        String sql = "select * from dc_well_info where id=" + wellId;
        List<Map<String, Object>> wellList = mapperUtils.queryBySql(sql);
        //获取井名
        String wellName = wellList.get(0).get("well_name").toString();
        if (wellList.size() == 0 || wellId == null) {
            return R.error("井不存在");
        }
        LasParser lasParser = new LasParser();
        if (file.isEmpty()) {
            return R.error("文件不能为空");
        }

        String fileName = file.getOriginalFilename();
        if (fileName == null || (!fileName.endsWith(".las") && !fileName.endsWith(".xlsx") && !fileName.endsWith(".xls"))) {
            return R.error("文件格式错误");
        }

        try {
            if (fileName.endsWith(".las")) {
                //解析las文件
                LasFile lasFile = lasParser.parse(file.getInputStream());
                if (lasFile == null) {
                    return R.error("文件为空");
                } else {
                    if (!lasFile.getWellInfo().get("WELL").value.equals(wellName)) {
                        return R.error("井名不一致,请重新上传");
                    }
                }
                return R.ok().put("data", lasFile);
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("文件解析失败: " + e.getMessage());
        }
    }
    @Operation(summary = "导入曲线数据")
    @PostMapping("/importCurveData")
    public R importCurveData(@RequestBody Map<String, Object> map) {
        //获取井ID
        Integer wellId = (Integer) map.get("wellId");
        //获取曲线数据
        Map<String, List<Double>> curveData = (Map<String, List<Double>>) map.get("curveData");
        //导入数据库
        curveDataService.addCurveData(curveData, wellId);
        return R.ok();
    }

    /**
     * 根据井ID查询曲线数据
     *
     * @param map
     * @return
     */
    @Operation(summary = "根据井ID查询曲线数据")
    @PostMapping("/getCurveData")
    public R getCurveData(@RequestBody Map<String, Integer> map) {
        //获取井ID
        Integer wellId = map.get("wellId");
        //获取分页参数
        Integer currentPage = map.get("currentPage");
        //获取每页显示条数
        Integer pageSize = map.get("pageSize");
        //获取总条数
        String sql = "select count(*) from dc_curvedata where well_id=" + wellId;
        List<Map<String, Object>> contList = mapperUtils.queryBySql(sql);
        Integer total = Integer.parseInt(contList.get(0).get("count").toString());
        //总页数
        Integer totalPage = total % pageSize == 0 ? total / pageSize : total / pageSize + 1;
        return R.ok().put("data", curveDataService.getCurveData(wellId, currentPage, pageSize)).put("total", total).put("totalPage", totalPage);
    }

    /**
     * 数据预处理
     *
     * @return
     */
    @Operation(summary = "数据预处理")
    @PostMapping("/prepData")
    public R prepData(@RequestParam("wellId") Integer wellId) {
        //把表中所有值为-9999或者为-999.25的值全部改为0.001
        String sql = "update dc_curvedata set " +
                "spdh=case when spdh in (-9999,-999.25) then 0.001 else spdh end," +
                "gr=case when gr in (-9999,-999.25) then 0.001 else gr end," +
                "azod=case when azod in (-9999,-999.25) then 0.001 else azod end," +
                "dazod= case when dazod in (-9999,-999.25) then 0.001 else dazod end," +
                "dt24= case when dt24 in (-9999,-999.25) then 0.001 else dt24 end," +
                "rd= case when rd in (-9999,-999.25) then 0.001 else rd end," +
                "rs= case when rs in (-9999,-999.25) then 0.001 else rs end," +
                "rmsf= case when rmsf in (-9999,-999.25) then 0.001 else rmsf end," +
                "cal1= case when cal1 in (-9999,-999.25) then 0.001 else cal1 end," +
                "zden= case when zden in (-9999,-999.25) then 0.001 else zden end," +
                "zcorr= case when zcorr in (-9999,-999.25) then 0.001 else zcorr end," +
                "pe= case when pe in (-9999,-999.25) then 0.001 else pe end," +
                "cn= case when cn in (-9999,-999.25) then 0.001 else cn end," +
                "cnc= case when cnc in (-9999,-999.25) then 0.001 else cnc end," +
                "cncf= case when cncf in (-9999,-999.25) then 0.001 else cncf end," +
                "ten= case when ten in (-9999,-999.25) then 0.001 else ten end," +
                "bs= case when bs in (-9999,-999.25) then 0.001 else bs end," +
                "cali= case when cali in (-9999,-999.25) then 0.001 else cali end," +
                "drhb= case when drhb in (-9999,-999.25) then 0.001 else drhb end," +
                "res_bd= case when res_bd in (-9999,-999.25) then 0.001 else res_bd end," +
                "res_bm= case when res_bm in (-9999,-999.25) then 0.001 else res_bm end," +
                "res_bs= case when res_bs in (-9999,-999.25) then 0.001 else res_bs end," +
                "rhob= case when rhob in (-9999,-999.25) then 0.001 else rhob end," +
                "tnph= case when tnph in (-9999,-999.25) then 0.001 else tnph end," +
                "tvd= case when tvd in (-9999,-999.25) then 0.001 else tvd end," +
                "devod= case when devod in (-9999,-999.25) then 0.001 else devod end " +
                "where well_id=" + wellId;
        mapperUtils.updataBySql(sql);
        return R.ok();
    }
}
